package br.com.vti.sisp_sicrim.antigo.util;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.ParseException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.swing.text.MaskFormatter;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.export.JRPdfExporter;

import org.apache.commons.lang3.StringUtils;

public class Reports {

	private int[] qtdcam = new int[100];
	private int pagesize = 60;
	private int linesize = 80;
	private int pagenumber = 1;
	private int pageline = 1;
	private String[][] campo = new String[100][100];
	private int[][] linha = new int[100][100];
	private int[][] coluna = new int[100][100];
	private int[][] tamanho = new int[100][100];
	private String[][] tipo = new String[100][100];
	private String[][] mascara = new String[100][100];
	private String[][] valor = new String[100][100];
	private String[][] titulo = new String[100][100];
	ReportDesigner reportDesigner = new ReportDesigner();
	JasperDesign design;
	JasperReport jr;
	List<LinhasRel> linhasRelatorio;
	JRBeanCollectionDataSource jrBean;
	Map<String, Object> parametros;
	JasperPrint impressao;

	public Reports() throws JRException {

		// preparar layout e conte�do
		design = reportDesigner.getDesign(pagesize);
		jr = JasperCompileManager.compileReport(design);
		linhasRelatorio = new LinkedList<LinhasRel>();
		parametros = new HashMap<String, Object>();
		Arrays.fill(qtdcam, 0);
		pagenumber = 1;
		pageline = 1;
	}

	public void adicionaCampo(int nroRel, int linha, int coluna, int tamanho, String campo, String tipo, String mascara, String titulo) {
		qtdcam[nroRel]++;
		if (qtdcam[nroRel] > 99)
			return;
		this.campo[nroRel][qtdcam[nroRel]] = campo;
		this.tipo[nroRel][qtdcam[nroRel]] = tipo;
		this.mascara[nroRel][qtdcam[nroRel]] = mascara;
		this.titulo[nroRel][qtdcam[nroRel]] = titulo;
		this.linha[nroRel][qtdcam[nroRel]] = linha;
		this.coluna[nroRel][qtdcam[nroRel]] = coluna;
		this.tamanho[nroRel][qtdcam[nroRel]] = tamanho;
	}

	public int localizaCampo(int nroRel, String campo) {
		int index = -1;
		for (int ind = 1; ind <= qtdcam[nroRel]; ind++) {
			if (this.campo[nroRel][ind].equals(campo))
				return ind;
		}
		return index;
	}

	public void adicionaValorReport(String campo, Object valor) throws ParseException {
		int nroRel, nroCam;
		for (nroRel = 1; nroRel <= 99; nroRel++) {
			nroCam = localizaCampo(nroRel, campo);
			if (nroCam != -1) {
				adicionaValor(nroRel, campo, valor);
			}

		}
	}

	public void adicionaValor(int nroRel, String campo, Object valor) throws ParseException {
		int index = localizaCampo(nroRel, campo);
		String strVal;
		if (index == -1)
			return;
		switch (tipo[nroRel][index]) {
		case "A":
		case "X": {
			strVal = String.valueOf(valor);
			if (!this.mascara[nroRel][index].equals("")) {
				MaskFormatter mask = new MaskFormatter(this.mascara[nroRel][index]);
				try {
					strVal = mask.valueToString(strVal);
				} catch (Exception e) {

				}

			} else {
				while (strVal.length() < this.tamanho[nroRel][index])
					strVal = strVal + " ";
				try {
					strVal = strVal.substring(0, this.tamanho[nroRel][index]);
				} catch (Exception e) {

				}
			}
			this.valor[nroRel][index] = strVal;
			break;
		}
		case "N":
		case "L": {
			strVal = String.valueOf(valor);
			if (!this.mascara[nroRel][index].equals("")) {
				MaskFormatter mask = new MaskFormatter(this.mascara[nroRel][index]);
				try {
					strVal = mask.valueToString(strVal);
				} catch (Exception e) {

				}
			} else {
				while (strVal.length() < this.tamanho[nroRel][index])
					strVal = " " + strVal;
				strVal = strVal.substring(0, this.tamanho[nroRel][index]);
			}
			this.valor[nroRel][index] = strVal;
			break;
		}
		default: {
			strVal = String.valueOf(valor);
			while (strVal.length() < this.tamanho[nroRel][index])
				strVal = strVal + " ";
			strVal = strVal.substring(0, this.tamanho[nroRel][index]);
			this.valor[nroRel][index] = strVal;
			break;
		}
		}
	}

	public void gravaRelatorio(int nroRel) {

		int linRel, ind;
		String linhaRel = "";
		ind = 1;
		linRel = linha[nroRel][ind];
		while (ind <= qtdcam[nroRel]) {
			if (pageline == 1) {
				// GRAVAR CABECALHO
			}
			while (pageline < linRel) {
				linhasRelatorio.add(new LinhasRel(""));
				pageline++;
			}
			linhaRel = StringUtils.repeat(" ", linesize + 1);
			while (linRel == linha[nroRel][ind] && ind <= qtdcam[nroRel]) {
				if (titulo[nroRel][ind].equals("")) {
					linhaRel = UtilBean.moveSubstring(linhaRel, coluna[nroRel][ind] - 1, tamanho[nroRel][ind], tipo[nroRel][ind], valor[nroRel][ind]);
				} else {
					linhaRel = UtilBean.moveSubstring(linhaRel, coluna[nroRel][ind] - 1, tamanho[nroRel][ind], "A", titulo[nroRel][ind]);
				}

				ind++;
			}
			linhasRelatorio.add(new LinhasRel(linhaRel));
			pageline++;
			if (pageline > pagesize) {
				// QUEBRA DE P�GINA
				pageline = 1;
				pagenumber++;
			}
			// ind++;
			linRel = linha[nroRel][ind];
		}
	}

	public void setPageSize(int pagesize) {
		this.pagesize = pagesize;
	}

	public void setLineSize(int linesize) {
		this.linesize = linesize;
	}

	public int getPageNumber() {
		return pagenumber;
	}

	public void imprimir() throws JRException, FileNotFoundException {
		jrBean = new JRBeanCollectionDataSource(linhasRelatorio);
		impressao = JasperFillManager.fillReport(jr, parametros, jrBean);
		OutputStream outputStream = new FileOutputStream(System.currentTimeMillis() + ".pdf");
		JRPdfExporter exporter = new JRPdfExporter();
		exporter.setParameter(JRExporterParameter.JASPER_PRINT, impressao);
		exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outputStream);
		exporter.exportReport();
	}
}
